/* @(#)OrderStateSync.java   
 *
 * Project: GS-1
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny Li    2006-9-4     新增
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.autotask;

import java.io.BufferedReader;

import java.io.InputStreamReader;
import java.net.URL;

import com.hitrust.gs.integration.GSApepInfoProcess;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.GSSystemConfigurator;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.exception.GSException;

/**
 * 
 */
public class OrderStateSync {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

    public void Sync() {

        BufferedReader in;
        String iContent = "";

        try {
            GSLogWriter.info("Begin to Down Order state synchronization Data... ");
            String tDay = DateUtil.getNextUpDay(-1);
            // 文件下载
            String Name = "/" + GSSystemConfigurator.getParam("gs.purcode") + "_JYPT_ItemSynch_" + tDay + ".txt";
            URL url = new URL(GSSystemConfigurator.getParam("gs.down.schema") + "://" + GSSystemConfigurator.getParam("gs.down.host") + ":"
                    + GSSystemConfigurator.getParam("gs.down.port") + "/" + GSSystemConfigurator.getParam("gs.down.fullpath") + Name);
            // url = new
            // URL("http://61.152.242.72:7001/v3/back/down/BSWZ_JYPT_ItemSynch_20060912.txt");

            in = new BufferedReader((new InputStreamReader(url.openStream(), "GB2312")));

            int errCnt = 0;
            if (in.ready()) {

                String instr = in.readLine();
                while (instr != null) {
                    GSLogWriter.debug(this, "client input is :" + instr);
                    iContent = iContent + instr;
                    int idx = iContent.toUpperCase().indexOf("&END@$#");
                    if (idx > -1) {
                        String rt = "";
                        iContent = iContent.substring(0, idx);
                        GSApepInfoProcess gsip = new GSApepInfoProcess();
                        GSLogWriter.info("process: " + iContent);
                        try {
                            rt = gsip.process(iContent);
                            GSLogWriter.info("result: " + rt);
                            if (!rt.equals("1")) {
                                errCnt++;
                            }
                        } catch (GSException e) {
                            errCnt++;
                            GSLogWriter.error(e.getMessage());
                        }
                        iContent = "";
                    }
                    instr = in.readLine();
                }
            }
            if (errCnt == 0) {
                GSLogWriter.info("Order state synchronization complete, NO ERROR!");
            } else {
                GSLogWriter.info("Order state synchronization complete, occur error " + errCnt + " times!");
            }
        } catch (Exception ex) {
            GSLogWriter.error("下载订单状态同步失败!");
            GSLogWriter.error(ex.getMessage());
        }

    }

}
